#include <stdio.h> 

static const int PAG_SHIFT = 13;
static const int PAG_SIZE  = 1 << PAG_SHIFT;
static const size_t MAX_SIZE  = 256 * 1024;

static int lg_floor(size_t bytes) {
    int log = 0;
    for (int i = 4; i >= 0; --i) {
        int shift = 1 << i;
        size_t x = bytes >> shift;
        if (x != 0) {
            bytes = x;
            log += shift;
        }
    }

    return log;
}

int calc_alignment(size_t bytes) {
    const int min_align = 16;
    int alignment = 8;

    if (bytes > MAX_SIZE)
        alignment = MAX_SIZE;
    else if (bytes >= 128)
        alignment = (1 << lg_floor(bytes)) / 8;
    else if (bytes >= min_align)
        alignment = min_align;

    if (alignment > PAG_SIZE)
        alignment = PAG_SHIFT;

    return alignment;
}

int main(int argc, char **argv) {
    int alignment = 8;
    int count = 0;
    for (size_t size = 8; size <= MAX_SIZE; size += alignment) {
        alignment = calc_alignment(size);

        printf("\t%08lu", size);
        if (++count % 8 == 0) printf("\n");
    }
    printf("\n%d\n", count);

    return 0;
}
